package com.java1234.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.java1234.dao.ILibrarianDao;
import com.java1234.model.Borrower;
import com.java1234.model.Librarian;
import com.java1234.util.StringUtil;

public class LibrarianImpl implements ILibrarianDao {
	/**
	 * 登录验证
	 * 
	 * @param con
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public Librarian login(Connection con, Librarian librarian) throws Exception {
		Librarian resultUser = null;
		String sql = "select * from t_librarian where librarianAccount=? and password=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, librarian.getLibrarianAccount());
		pstmt.setString(2, librarian.getPassword());
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			resultUser = new Librarian();
			resultUser.setId(rs.getInt("id"));
			resultUser.setLibrarianAccount(rs.getString("librarianAccount"));
			resultUser.setLibrarianName(rs.getString("librarianName"));
			resultUser.setPassword(rs.getString("password"));

		}
		con.close();
		return resultUser;
	}

	/**
	 * 数据库图书管理员信息查询
	 * 
	 * @param con
	 * @param book
	 * @return
	 * @throws Exception
	 */
	public ResultSet list(Connection con, Librarian librarian) throws Exception {
		StringBuffer sb = new StringBuffer("select * from t_librarian");
		if (StringUtil.isNotEmpty(librarian.getLibrarianAccount())) {
			sb.append(" and librarianAccount like '%" + librarian.getLibrarianAccount() + "%'");
		}
		if (StringUtil.isNotEmpty(librarian.getLibrarianName())) {
			sb.append(" and librarianName like '%" + librarian.getLibrarianName() + "%'");
		}
		if (StringUtil.isNotEmpty(librarian.getPassword())) {
			sb.append(" and password like '%" + librarian.getPassword() + "%'");
		}
		PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		ResultSet i = pstmt.executeQuery();
		return i;
	}

	/**
	 * 数据库图书管理员信息添加
	 */
	public int add(Connection con, Librarian librarian) throws Exception {
		String sql = "insert into t_librarian values(?,?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, librarian.getId());
		pstmt.setString(2, librarian.getLibrarianAccount());
		pstmt.setString(3, librarian.getLibrarianName());
		pstmt.setString(4, librarian.getPassword());
		int i = pstmt.executeUpdate();

		return i;
	}

	/**
	 * 数据库图书管理员信息修改
	 */
	public int update(Connection con, Librarian librarian) throws Exception {
		String sql = "update t_librarian set librarianName=?,password=? where librarianAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, librarian.getLibrarianName());
		pstmt.setString(2, librarian.getPassword());
		pstmt.setString(3, librarian.getLibrarianAccount());
		int i = pstmt.executeUpdate();
		return i;
	}

	/**
	 * 数据库图书管理员信息删除
	 * 
	 * @param con
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(Connection con, String librarianAccount) throws Exception {
		String sql = "delete from t_librarian where librarianAccount=?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, librarianAccount);
		int i = pstmt.executeUpdate();

		return i;
	}
}
